<template>
    <div id="login">
        <el-card style="width: 400px;margin: 0 auto;">
            <div slot="header">
                <h4>登录</h4>
            </div>
            <el-form :model="user" :rules="loginRule" ref="loginForm" class="demo-ruleForm">
                <el-form-item label="账号" prop="account">
                    <el-input v-model="user.account"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="login">登录</el-button>
                    <router-link to="/register" style="margin-left: 20px;">去注册</router-link>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>

    export default {
        name: "",
        data() {
            return {
                user: {},
                loginRule: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ]
                }
            };
        },
        created() {

        },
        methods: {
            login() {
                this.$refs.loginForm.validate((r) => {
                    if (r) {
                        this.$axios.post("/api/user/login", this.user).then(res => {
                            console.log(res);
                            if (res.data.status === 1) {
                                localStorage.setItem("TOKEN", res.data.data.token);
                                this.$router.push("/");
                            } else {
                                this.$message.warning(res.data.msg);
                            }
                        })
                    } else {
                        this.$message.error('请填写完整信息');
                    }
                })
            }
        }
    }
</script>

<style lang="scss">
#login {

  input {
    background: black;
  }


}
</style>